import { HttpErrorResponse } from '@angular/common/http';
import { Component, OnInit } from '@angular/core';
import { Title } from '@angular/platform-browser';
import { ActivatedRoute, Router } from '@angular/router';
import { BoardGroupCollectVo } from 'src/app/model/board-group-collect-vo';
import { BoardGroupService } from 'src/app/services/board-group.service';
import { BtnNaviSharedService } from 'src/app/services/btn-navi-shared.service';

@Component({
  selector: 'app-board-group-detail',
  templateUrl: './board-group-detail.component.html',
  styles: [``]
})
/**
 * 版块组主页
 * @constructor
 */
export class BoardGroupDetailComponent implements OnInit {
  public item!: BoardGroupCollectVo;
  public errorMessage: string = '';

  constructor(
    private title: Title,
    private router: Router,
    private activatedRoute: ActivatedRoute,
    private boardGroupService: BoardGroupService,
    private btnNaviSharedService: BtnNaviSharedService) {
    this.btnNaviSharedService.active(false);//不需要全局的导航
  }

  ngOnInit(): void {
    let boardGroupConstr = this.activatedRoute.snapshot.paramMap.get('constr') ?? '-';
    let z: number = this.parseConString(boardGroupConstr);

    this.boardGroupService.getCascadeUsedBoard(z).subscribe({
      next: response => {
        this.item = response;
        this.title.setTitle(this.item.boardGroupVo.title);
      },
      error: err => {
        if (err instanceof HttpErrorResponse) {
          if (err.error instanceof ErrorEvent) {
            this.errorMessage = err.error.message;
          } else {
            if (err.status === 404) {
              //跳转
              this.router.navigate(['404']);
            } else {
              this.errorMessage = err.error;
            }
          }
        } else if (err instanceof Error) {
          this.errorMessage = err.message;
        } else { this.errorMessage = err; }
        //ETC
      },
      complete: () => {
        console.log('[BoardGroup-CTL]Completed');
      }
    });
  }
  /**
   * 从地址(board/volumes/:constr)中提取相关主键
   * @param constr 
   */
  private parseConString(constr: string): number {
    if (typeof constr != 'undefined' && constr) {
      let z: string = constr.replace('.xhtml', '');
      return parseInt(z) ?? 0;
    }
    return 0;
  }
}
